package airthmetic.exercise.sort.cmp;


// 插入排序 AC   有时未AC 超时
public class _912_排序数组_Insert3 {
    public int[] sortArray(int[] nums) {
        if (nums == null || nums.length < 2) {
            return nums;
        }
        int n = nums.length;
        int pos;
        for(int i=1; i<n; i++){
            pos = i-1;
            int currentNum = nums[i];
            while(pos>=0 && nums[pos] > currentNum){
                nums[pos + 1] = nums[pos];
                pos--;
            }
            nums[pos+1] = currentNum;
        }

        return nums;

    }

    public void swap(int[] nums, int left, int right) {
        if (left == right) {
            return;
        }
        nums[left] ^= nums[right];
        nums[right] ^= nums[left];
        nums[left] ^= nums[right];
    }
}
